module ActiveTest::Behaviour
  module Authorisation
    
    def self.included(base)
      base.extend(ClassMethods)
    end
    
    module ClassMethods
      
      # Generates a test case which checks a given action requires special permissions.
      # This is a negative test case.
      #
      # Options:
      # * +:parameters+ -- a hash of parameters to be passed as part of the request.
      # * +:login_as+ -- user to login as for this test case (takes a symbol)
      #
      def requires_permission_on(action, options = {})
        define_behavioral(:require_permission_on, action, options) do
          login_as(options[:login_as]) if options[:login_as]
          assert_requires_permission { send("call_#{action}_action", options) }
        end
      end
    
      # Generates a test case which checks a given action requires special permissions.
      # This is a negative test case.
      #
      # Options:
      # * +:parameters+ -- a hash of parameters to be passed as part of the request.
      # * +:login_as+ -- user to login as for this test case (takes a symbol)
      #
      def accepts_permission_on(action, options = {})
        define_behavioral(:accept_permission_on, action, options) do
          login_as(options[:login_as]) if options[:login_as]
          assert_accepts_permission { send("call_#{action}_action", options) }
        end
      end
    end
    
  end
end